package com.filter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.util.GlobalUtil;

//登陆过滤器
@WebFilter
public class LoginFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("-----------过滤器初始化------------");
	}
		
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			    FilterChain chain) throws IOException, ServletException {	
		
		   HttpServletRequest request = (HttpServletRequest) req;
		   String urlName=request.getRequestURI();	
		   //把ServletResponse强转成HttpServletResponse
		   HttpServletResponse response = (HttpServletResponse) resp;
		   //判断是不是登陆url
		   if (!urlName.endsWith(GlobalUtil.ADMIN_LOGIN_VIEW)) {
			   //判断是不是访问服务的url
			   if (urlName.endsWith(".htm") || urlName.endsWith(".jsp")) {				   
				   System.out.println("过滤,处理++++++++"+urlName);	
				   request.setCharacterEncoding("UTF-8");		   
				   response.setCharacterEncoding("UTF-8");
				   response.setContentType("text/html;charset=UTF-8");				   
		            //判断session对象中是否有登陆对象
					 if (request.getSession().getAttribute(GlobalUtil.ADMIN_IN_SESSION)== null || request.getSession() ==null ) {				        	  						 						 					   
						 //1.得到用户带过来的authlogin的cookie
					        String value = null;
					        Cookie cookies[] = request.getCookies();
					        for(int i=0;cookies!=null && i<cookies.length;i++){
					            if(cookies[i].getName().equals("autologin")){
					                value = cookies[i].getValue();
					            }
					        }
					        //2.得到 cookie中的用户名和密码 
					        if(value!=null){
					            String username = value.split("\\.")[0];
					            String password = value.split("\\.")[1];
					            
					            //3.调用dao获取用户对应的密码
					          //  UserInfoVo dao = new UserInfoVo();
					          //  UserInfoVo user = dao.find(username);
					          //  String dbpassword = user.getPassword();
					            
					            //4.检查用户带过来的md5的密码和数据库中的密码是否匹配,如匹配则自动登陆
					            //if(password.equals(WebUtils.md5(dbpassword))){
					            //    request.getSession().setAttribute("user", user);
					           // }
					            request.getSession().setAttribute(GlobalUtil.ADMIN_IN_SESSION, "sssdd");
					            chain.doFilter(request, response);  //让目标资源执行，放行
					            return;
					        }
						 
						 //没有登陆就跳转登陆页面						 						 
						 //response.sendRedirect(request.getContextPath()+"/login.jsp");
						 //request.getRequestDispatcher("/login.jsp").forward(request, response);
						 //跳出二级界面,到首页
					     PrintWriter out = response.getWriter();  
				            out.println("<html>");  
				            out.println("<script>"); 				            
				            out.println("window.open ('/MyWeb-SpringMVC-MyBatis-EasyUI','_top')");  
				            out.println("</script>");  
				            out.println("</html>");						
							return;
					  }
					 
				   }else {
					   System.out.println("不过滤，不处理++++++++"+urlName);
				   }			   
			   } 		  
		  // System.out.println("Filter执行前！！！========");		   			
		   chain.doFilter(request, response);  //让目标资源执行，放行
		 //  System.out.println("Filter执行后！！！=========");		  		  			   
	}
	
	@Override
	public void destroy() {
		System.out.println("-----------过滤器销毁------------");
		
	}

}
